package sv.com.seguros.dao.asg;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import sv.com.seguros.dao.BaseDaoImpl;
import sv.com.seguros.dto.asg.AsgContacto;
import sv.com.seguros.exception.WebException;

public class AsgContactoDaoImpl extends BaseDaoImpl implements AsgContactoDao {
	
	@SuppressWarnings("unchecked")
	public List<Object[]> findAllByType(Long identifier, String type) throws WebException {
		try {
			DetachedCriteria criteria = DetachedCriteria.forClass(AsgContacto.class);
			criteria.createAlias("ctgTipoContacto", "ctgTipoContacto", Criteria.INNER_JOIN);
			criteria.setProjection(Projections.projectionList().
				add(Projections.property("asgContactoId")).
				add(Projections.property("asgContactoNombre")).
				add(Projections.property("asgContactoTelefono")).
				add(Projections.property("asgContactoTelefono2")).
				add(Projections.property("asgContactoTelefonoExtension")).
				add(Projections.property("asgContactoCorreo")).
				add(Projections.property("ctgTipoContacto.ctgCatalogoId")).
				add(Projections.property("ctgTipoContacto.ctgCatalogoNombre"))
			);
			if("reclamo".equalsIgnoreCase(type))
				criteria.add(Restrictions.eq("asgReclamo.asgReclamoId", identifier));
			else if("pendiente".equalsIgnoreCase(type))
				criteria.add(Restrictions.eq("asgPendiente.asgPendienteId", identifier));
			else if("venta".equalsIgnoreCase(type))
				criteria.add(Restrictions.eq("asgVenta.asgVentaId", identifier));
			criteria.addOrder(Order.asc("asgContactoNombre"));
			return (List<Object[]>) findByCriteria(criteria);
		} catch (Exception e) {
			throw new WebException("Problemas tratar de obtener el detalle de reclamos");
		}
	}

}
